home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio / Ham Radio CD-ROM (Emerald Software) (1995).ISO / misc / 9q920411 / hs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-03  |  17.5 KB  |  718 lines

  1. /* Interface driver for the DRSI PCPA or the Eagle 8530 boards for the IBM PC
  2.  * connected to a WA4DSY 56kbps modem. Uses polling-loop transfers with
  3.  * interrupts disabled for maximum speed.
  4.  *
  5.  * This driver is a bit of a kludge. A DMA-driven card and driver (e.g.,
  6.  * the PI) is much better, but this is better than nothing if all you have
  7.  * is a "dumb" 8530 card.
  8.  *
  9.  * Copyright 1991 Phil Karn, KA9Q
  10.  */
  11. #include <stdio.h>
  12. #include <dos.h>
  13. #include "global.h"
  14. #include "mbuf.h"
  15. #include "iface.h"
  16. #include "pktdrvr.h"
  17. #include "netuser.h"
  18. #include "hs.h"
  19. #include "8530.h"
  20. #include "ax25.h"
  21. #include "trace.h"
  22. #include "pc.h"
  23. #include "proc.h"
  24. #include "devparam.h"
  25.  
  26. static void flushrx __ARGS((int16 data));
  27. static void hdlcparam __ARGS((struct hdlc *hp));
  28. static void hexint __ARGS((struct hdlc *hp));
  29. static void hrxint __ARGS((struct hdlc *hp));
  30. static int hs_stop __ARGS((struct iface *iface));
  31. static int hs_raw __ARGS((struct iface *iface,struct mbuf *bp));
  32. static int32 hs_ctl __ARGS((struct iface *,int cmd,int set,int32 val));
  33. static void hstxoff __ARGS((struct hdlc *hp));
  34. static void hstxon __ARGS((struct hdlc *hp));
  35. static void htxint __ARGS((struct hdlc *hp));
  36. static void init_delay __ARGS((void));
  37. static void msdelay __ARGS((void));
  38.  
  39. static struct hs Hs[NHS];
  40. static INTERRUPT (*Hshandle[])() = { hs0vec };
  41. static struct hdlc Hdlc[2*NHS];
  42. static int16 Nhs;
  43.  
  44. /* Master interrupt handler for the PC-100 card. All interrupts come
  45.  * here first, then are switched out to the appropriate routine.
  46.  */
  47. INTERRUPT (far *(hsint)(dev))()
  48. int dev;
  49. {
  50.     register char iv;
  51.     int16 hsbase;
  52.     struct hs *hsp;
  53.     register struct hdlc *hp;
  54.     
  55.     hsp = &Hs[dev];
  56.     hsp->ints++;
  57.     hsbase = hsp->addr;
  58.  
  59. #ifdef    foo
  60.     outportb(hsbase+4,0x8+0x10);    /* HIT EAGLE INTACK */
  61.     (void)inportb(hsbase+CHANA+CTL,R0);
  62.     outportb(hsbase+4,0x8);        /***/
  63. #endif
  64.  
  65.     /* Read interrupt status from channel A */
  66.     while((iv = read_scc(hsbase+CHANA+CTL,R3)) != 0){
  67.         if(iv & CHARxIP){
  68.             /* Channel A Rcv Interrupt Pending */
  69.             hp = &Hdlc[2*dev];
  70.             hrxint(hp);
  71.         } else if(iv & CHATxIP){
  72.             /* Channel A Transmit Int Pending */
  73.             hp = &Hdlc[2*dev];
  74.             htxint(hp);
  75.         } else if(iv & CHAEXT){
  76.             /* Channel A External Status Int */
  77.             hp = &Hdlc[2*dev];
  78.             hexint(hp);
  79.         } else if(iv & CHBRxIP){
  80.             /* Channel B Rcv Interrupt Pending */
  81.             hp = &Hdlc[(2*dev)+1];
  82.             hrxint(hp);
  83.         } else if(iv & CHBTxIP){
  84.             /* Channel B Transmit Int Pending */
  85.             hp = &Hdlc[(2*dev)+1];
  86.             htxint(hp);
  87.         } else if(iv & CHBEXT){
  88.             /* Channel B External Status Int */
  89.             hp = &Hdlc[(2*dev)+1];
  90.             hexint(hp);
  91.         }
  92.         /* Reset interrupt pending state */
  93.         write_scc(hp->ctl,R0,RES_H_IUS);
  94.         outportb(hsbase+CHANA+CTL,0);    /* Restore pointer to 0 */
  95.         outportb(hsbase+CHANB+CTL,0);    /* Restore pointer to 0 */
  96.     }
  97.     outportb(hsbase+CHANA+CTL,0);    /* Restore pointer to 0 */
  98.     outportb(hsbase+CHANB+CTL,0);    /* Restore pointer to 0 */
  99.     return hsp->chain ? hsp->save.vec : NULL;
  100. }
  101. /* HDLC SIO External/Status interrupts
  102.  * The only one that can happen in this driver is a DCD change
  103.  */
  104. static void
  105. hexint(hp)
  106. register struct hdlc *hp;
  107. {
  108.     struct mbuf *rcvbuf;
  109.     char *cp;
  110.     int cnt,data;
  111.     register int ctl;
  112.  
  113.     ctl = hp->ctl;
  114.     data = hp->data;
  115.     hp->exints++;
  116.  
  117.     /* Allocate a receive buffer */
  118.     if((rcvbuf = alloc_mbuf(hp->bufsiz+sizeof(struct phdr))) == NULLBUF){
  119.         /* Alloc failed; refuse to proceed */
  120.         hp->nomem++;
  121.         write_scc(ctl,R3,ENT_HM|RxENABLE|RxCRC_ENAB|Rx8);
  122.         write_scc(ctl,R0,RES_EXT_INT);
  123.         return;
  124.     }
  125.     /* Allow space for phdr descriptor on front */
  126.     rcvbuf->data += sizeof(struct phdr);
  127.     cnt = 0;
  128.  
  129.     /* Disable DCDIE bit so we can track changes in DCD */
  130.     write_scc(ctl,R15,0);
  131.  
  132.     write_scc(ctl,R3,ENT_HM|RxENABLE|RxCRC_ENAB|Rx8);
  133.     flushrx(data);
  134.     while((cnt = rx8530(ctl,data,cp,hp->bufsiz)) != -1){
  135.         if(cnt > 4){
  136.             /* Good frame */
  137.             hp->good++;
  138.             /* Toss crc */
  139.             rcvbuf->cnt = cnt - 1;
  140.             net_route(hp->iface,CL_AX25,rcvbuf);
  141.             /* Replenish buffer */
  142.             rcvbuf = alloc_mbuf(hp->bufsiz + sizeof(struct phdr));
  143.         }
  144.         /* Start new buffer */
  145.         if(rcvbuf == NULLBUF)
  146.             break;    /* alloc failed */
  147.         rcvbuf->data +=  sizeof(struct phdr);
  148.     }    
  149.     write_scc(ctl,R0,RES_EXT_INT);
  150.     write_scc(ctl,R15,DCDIE);    /* Re-enable DCD */
  151.     write_scc(ctl,R3,ENT_HM|RxENABLE|RxCRC_ENAB|Rx8);
  152.  
  153.     /* Get rid of fragmentary buffer */
  154.     free_p(rcvbuf);
  155. }
  156. static void
  157. flushrx(data)
  158. register int16 data;
  159. {
  160.     register int i = 5;
  161.     while(i-- != 0)
  162.         (void)inportb(data);
  163. }
  164. /* HDLC receiver interrupt handler.
  165.  * Not used in this driver
  166.  */
  167. static void
  168. hrxint(hp)
  169. register struct hdlc *hp;
  170. {
  171. }
  172. /* HDLC transmit interrupt service routine
  173.  * Not used in this driver
  174.  */
  175. static void
  176. htxint(hp)
  177. register struct hdlc *hp;
  178. {
  179. }
  180.  
  181. /* (re)Initialize HDLC controller parameters */
  182. static void
  183. hdlcparam(hp)
  184. register struct hdlc *hp;
  185. {
  186.     char i_state;
  187.     register int16 ctl;
  188.  
  189.     /* Initialize 8530 channel for SDLC operation */
  190.     ctl = hp->ctl;
  191.     i_state = dirps();
  192.  
  193. #ifdef    foo
  194.     switch(ctl & 2){
  195.     case CHANA:
  196.         write_scc(ctl,R9,CHRA);    /* Reset channel A */
  197.         break;
  198.     case CHANB:
  199.         write_scc(ctl,R9,CHRB);    /* Reset channel B */
  200.         break;
  201.     }
  202.     pause(1L);    /* Allow plenty of time for resetting */
  203. #endif
  204.  
  205.     /* Deselect interrupts for now */
  206.     write_scc(ctl,R1,0);
  207.     write_scc(ctl,R15,0);
  208.  
  209.     /* X1 clock, SDLC mode, Sync modes enable, parity disable */
  210.     write_scc(ctl,R4,X1CLK | SDLC | SYNC_ENAB);
  211.  
  212.     /* CRC preset 1, NRZ encoding, no active on poll, flag idle,
  213.      * flag on underrun, no loop mode, 8 bit sync
  214.      */
  215.     write_scc(ctl,R10,CRCPS|NRZ);
  216.  
  217.     /* 8530 gets both tx and rx clock from modem.
  218.      * By default, TRxC = transmit clock, RTxC = receive clock
  219.      * (swapped 11 Feb 1990 to use new DRSI wiring) UNLESS
  220.      * the 'r' parameter is specified
  221.      */
  222.     if(!hp->clkrev)
  223.         write_scc(ctl,R11,RCRTxCP | TCTRxCP);
  224.     else
  225.         write_scc(ctl,R11,RCTRxCP | TCRTxCP);
  226.  
  227.     /* Note: baud rate generator not used */
  228.  
  229.     /* Null out SDLC start address */
  230.     write_scc(ctl,R6,0);
  231.  
  232.     /* SDLC flag */
  233.     write_scc(ctl,R7,FLAG);
  234.  
  235.     /* DTR On, 8 bit TX chars, no break, TX enable, SDLC CRC,
  236.      * RTS off, TxCRC enable
  237.      */
  238.     write_scc(ctl,R5,DTR|Tx8|TxENAB|TxCRC_ENAB);
  239.  
  240.     /* 8 bit RX chars, auto enables off, no hunt mode, RxCRC enable,
  241.      * no address search, no inhibit sync chars, disable RX. Rx is
  242.      * started only by an actual DCD interrupt
  243.      */
  244.     write_scc(ctl,R3,RxENABLE|RxCRC_ENAB|Rx8);
  245.  
  246.     /* Dummy interrupt vector
  247.      * (This probably isn't necessary)
  248.      */
  249.     write_scc(ctl,R2,0);
  250.  
  251.     /* Enable only the external interrupts (modem interrupts) since
  252.      * polling is used for all actual tx/rx operations
  253.      */
  254.     write_scc(ctl,R1,EXT_INT_ENAB);
  255.  
  256.     /* Enable only DCD interrupts */
  257.     write_scc(ctl,R15,DCDIE);
  258.  
  259.     /* No reset, status low, master int enable, enable lower chain,
  260.      * no vector
  261.      */
  262.     write_scc(ctl,R9,MIE|NV);
  263.  
  264.     restore(i_state);
  265. }
  266. /* Attach a high speed iterface to the system
  267.  * argv[0]: hardware type, must be "hs"
  268.  * argv[1]: I/O address, e.g., "0x380"
  269.  * argv[2]: vector, e.g., "2"
  270.  * argv[3]: mode, must be "ax25"
  271.  * argv[4]: interface base label, e.g., "drsi0". Driver appends "a" and "b".
  272.  * argv[5]: receiver packet buffer size in bytes
  273.  * argv[6]: maximum transmission unit, bytes
  274.  * argv[7]: keyup delay, milliseconds
  275.  * argv[8]: persistence value, 0-255
  276.  * argv[9]: "r" to reverse sense of clock leads (optional)
  277.  */
  278. int
  279. hs_attach(argc,argv,p)
  280. int argc;
  281. char *argv[];
  282. void *p;
  283. {
  284.     register struct iface *if_hsa,*if_hsb;
  285.     struct hdlc *hp;
  286.     int dev;
  287.  
  288.     if(Nhs >= NHS){
  289.         tprintf("Too many hs controllers\n");
  290.         return -1;
  291.     }
  292.     if(if_lookup(argv[4]) != NULLIF){
  293.         tprintf("Interface %s already exists\n",argv[4]);
  294.         return -1;
  295.     }
  296.     dev = Nhs++;
  297.  
  298.     /* Initialize hardware-level control structure */
  299.     Hs[dev].addr = htoi(argv[1]);
  300.     Hs[dev].vec = atoi(argv[2]);
  301.     if(strchr(argv[2],'c') != NULLCHAR)
  302.         Hs[dev].chain = 1;
  303.     else
  304.         Hs[dev].chain = 0;
  305.  
  306.     /* Save original interrupt vector */
  307.     Hs[dev].save.vec = getirq(Hs[dev].vec);
  308.     /* Set new interrupt vector */
  309.     if(setirq(Hs[dev].vec,Hshandle[dev]) == -1){
  310.         tprintf("IRQ %u out of range\n",Hs[dev].vec);
  311.         Nhs--;
  312.         return -1;
  313.     }
  314.     /* Create interface structures and fill in details */
  315.     if_hsa = (struct iface *)callocw(1,sizeof(struct iface));
  316.     if_hsb = (struct iface *)callocw(1,sizeof(struct iface));
  317.  
  318.     if_hsa->addr = if_hsb->addr = Ip_addr;
  319.     if_hsa->name = mallocw(strlen(argv[4])+2);
  320.     strcpy(if_hsa->name,argv[4]);
  321.     strcat(if_hsa->name,"a");
  322.     if_hsb->name = mallocw(strlen(argv[4])+2);
  323.     strcpy(if_hsb->name,argv[4]);
  324.     strcat(if_hsb->name,"b");
  325.     if_hsb->mtu = if_hsa->mtu = atoi(argv[6]);
  326.     if_hsb->type = if_hsa->type = CL_AX25;
  327.     if_hsa->dev = 2*dev;
  328.     if_hsb->dev = 2*dev + 1;
  329.     if_hsb->stop = if_hsa->stop = hs_stop;
  330.     if_hsb->output = if_hsa->output = ax_output;
  331.     if_hsb->raw = if_hsa->raw = hs_raw;
  332.     if_hsa->ioctl = if_hsb->ioctl = hs_ctl;
  333.  
  334.     if(strcmp(argv[3],"ax25") == 0){
  335.         if(Mycall[0] == '\0'){
  336.             tprintf("set mycall first\n");
  337.             free((char *)if_hsa);
  338.             free((char *)if_hsb);
  339.             return -1;
  340.         }        
  341.         if_hsb->send = if_hsa->send = ax_send;
  342.         if(if_hsb->hwaddr == NULLCHAR)
  343.             if_hsb->hwaddr = mallocw(AXALEN);
  344.         memcpy(if_hsb->hwaddr,Mycall,AXALEN);
  345.         if(if_hsa->hwaddr == NULLCHAR)
  346.             if_hsa->hwaddr = mallocw(AXALEN);
  347.         memcpy(if_hsa->hwaddr,Mycall,AXALEN);
  348.     } else {
  349.         tprintf("Mode %s unknown for interface %s\n",
  350.             argv[3],argv[4]);
  351.         free((char *)if_hsa);
  352.         free((char *)if_hsb);
  353.         return -1;
  354.     }
  355.     if_hsa->next = if_hsb;
  356.     if_hsb->next = Ifaces;
  357.     Ifaces = if_hsa;
  358.  
  359.     write_scc(Hs[dev].addr+CHANA+CTL,R9,FHWRES);
  360.     hp = &Hdlc[2*dev+1];
  361.     hp->ctl = Hs[dev].addr + CHANB + CTL;
  362.     hp->data = Hs[dev].addr + CHANB + DATA;
  363.     hp->bufsiz = atoi(argv[5]);
  364.     if(argc > 7)
  365.         hp->txdelay = atol(argv[7]);
  366.     else
  367.         hp->txdelay = 15L;
  368.     if(argc > 8)
  369.         hp->p = atoi(argv[8]);
  370.     else
  371.         hp->p = 64;
  372.     if(argc > 9 && argv[9][0] == 'r')
  373.         hp->clkrev = 1;
  374.     else
  375.         hp->clkrev = 0;
  376.     hp->iface = if_hsb;
  377.     hdlcparam(hp);
  378.  
  379.     hp = &Hdlc[2*dev];
  380.     hp->ctl = Hs[dev].addr + CHANA + CTL;
  381.     hp->data = Hs[dev].addr + CHANA + DATA;
  382.     hp->bufsiz = atoi(argv[5]);
  383.     hp->txdelay = Hdlc[2*dev+1].txdelay;
  384.     hp->p = Hdlc[2*dev+1].p;
  385.     if(argc > 9 && argv[9][0] == 'r')
  386.         hp->clkrev = 1;
  387.     else
  388.         hp->clkrev = 0;
  389.     hp->iface = if_hsa;
  390.     hdlcparam(hp);
  391.  
  392.     outportb(Hs[dev].addr + 4,0x08);    /*EAGLE INT GATE */
  393.     /* Clear mask (enable interrupt) in 8259 interrupt controller */
  394.     maskon(Hs[dev].vec);
  395.  
  396.     /* Initialize timing delay loop */
  397.     init_delay();
  398.     if_hsa->txproc = newproc("hs tx",512,if_tx,0,if_hsa,NULL,0);
  399.     if_hsb->txproc = newproc("hs tx",512,if_tx,0,if_hsb,NULL,0);
  400.  
  401.     return 0;
  402. }
  403. static int
  404. hs_stop(iface)
  405. struct iface *iface;
  406. {
  407.     int dev;
  408.  
  409.     dev = iface->dev;
  410.     if(dev & 1)
  411.         return -1;    /* Valid only for the first device */
  412.     dev >>= 1;    /* Convert back into hs number */
  413.  
  414.     /* Turn off interrupts */
  415.     maskoff(Hs[dev].vec);
  416.  
  417.     /* Restore original interrupt vector */
  418.     setirq(Hs[dev].vec,Hs[dev].save.vec);
  419.  
  420.     /* Force hardware reset */
  421.     write_scc(Hs[dev].addr + CHANA+CTL,R9,FHWRES);
  422.     return 0;
  423. }
  424. /* Send raw packet */
  425. static int
  426. hs_raw(iface,bp)
  427. struct iface *iface;
  428. struct mbuf *bp;
  429. {
  430.  
  431.     struct hdlc *hp;
  432.     struct mbuf *nbp;
  433.     register int16 cnt;
  434.     register char *cp;
  435.     int16 ctl,data;
  436.  
  437.     dump(iface,IF_TRACE_OUT,CL_AX25,bp);
  438.     iface->rawsndcnt++;
  439.     iface->lastsent = secclock();
  440.     hp = &Hdlc[iface->dev];
  441.     hp->txpkts++;
  442.  
  443.     ctl = hp->ctl;
  444.     data = hp->data;
  445.  
  446.     cnt = len_p(bp);
  447.     /* If buffer isn't contiguous (which is almost always
  448.      * the case) copy it to a new buffer for speed
  449.      */
  450.     if(bp->next != NULLBUF){
  451.         if((nbp = copy_p(bp,cnt)) == NULLBUF){
  452.             hp->nomem++;
  453.             free_p(bp);
  454.             return -1;
  455.         }
  456.         free_p(bp);
  457.         bp = nbp;
  458.     }
  459.     cp = bp->data;
  460.     /* Turn transmitter on */
  461.     hstxon(hp);
  462.     /* Initialize transmitter CRC */
  463.     write_scc(ctl,R0,RES_Tx_CRC);
  464.     for(;;){
  465.         /* Wait for the transmitter to become ready */
  466.         while(!(inportb(ctl) & Tx_BUF_EMP))
  467.             ;
  468.         if(cnt-- == 0)
  469.             break;
  470.         outportb(data,*cp++); /* Send the character */
  471.     }
  472.     write_scc(ctl,R0,RES_EOM_L);    /* Allow CRC generation */
  473.     /* End of frame. Wait for TxEOM to go high, indicating start of
  474.      * CRC transmission. Note that we don't reset the transmit
  475.      * interrupt pending flag as one ordinarily would, since we're
  476.      * not using tx interrupts.
  477.      */
  478.     while(!(inportb(ctl) & TxEOM))
  479.         ;
  480.  
  481.     free_p(bp);
  482.     hstxoff(hp);    /* Shut down tx */
  483.     /* Hold off to give other guy a chance to
  484.      * respond
  485.      */
  486.     hp->deftime = msclock() + hp->txdelay + 500;
  487.     return 0;
  488. }
  489.  
  490. /* Turn on high speed transmitter. Does p-persistence, then sends a dummy
  491.  * frame to allow for keyup delay. Returns with transmitter on and interrupts
  492.  * disabled
  493.  */
  494. static void
  495. hstxon(hp)
  496. struct hdlc *hp;
  497. {
  498.     int16 ctl;
  499.     int i;
  500.     long ca;
  501.     int32 t;
  502.  
  503.     ctl = hp->ctl;
  504.  
  505.     /* Defer logic. Wait until deftime is in the past (so we
  506.      * defer to any overheard CTS messages) AND the p-persistence
  507.      * dice roll succeeds. The computation of ca allows for clock
  508.      * rollover (which happens every 49+ days).
  509.      */
  510.     for(;;){
  511.         t = msclock();
  512.         ca = hp->deftime - t;
  513.         if(ca > 0){
  514.             pause(ca);
  515.             continue;
  516.         }
  517.         hp->deftime = t;    /* Keep from getting too old */
  518.         if((rand() & 0xff) > uchar(hp->p)){
  519.             pause((long)MSPTICK);
  520.             continue;
  521.         }
  522.         break;
  523.     }
  524.     /* Prevent distractions. In particular, block off the DCD interrupt
  525.      * so we don't hear our own carrier and hang in the interrupt handler!
  526.      * Note that simply disabling CPU interrupts isn't enough since
  527.      * the call to pause will block and the kernel will re-enable
  528.      * them.
  529.      */
  530.     write_scc(ctl,R9,0);    /* Disable all SCC interrupts */
  531.     (void)dirps();        /* Return value is always 1 */
  532.  
  533.     /* Turn on carrier, enable transmitter */
  534.     write_scc(ctl,R5,TxCRC_ENAB | RTS | TxENAB | Tx8 | DTR);
  535.  
  536.     /* Delay for keyup interval */
  537.     for(i=hp->txdelay;i != 0;i--)
  538.         msdelay();
  539.  
  540. }
  541. /* Turn transmitter off at the end of a series of frames */
  542. static void
  543. hstxoff(hp)
  544. struct hdlc *hp;
  545. {
  546.     int cnt;
  547.     int16 ctl,data;
  548.  
  549.     ctl = hp->ctl;
  550.     data = hp->data;
  551.     /* To allow the SCC buffering to drain, we begin a dummy frame,
  552.      * then abort it
  553.      */
  554.     for(cnt=5;cnt != 0;cnt--){
  555.         while(!(inportb(ctl) & Tx_BUF_EMP))
  556.             ;
  557.         outportb(data,0);
  558.     }
  559.     write_scc(ctl,R0,SEND_ABORT);
  560.  
  561.     /* Turn off carrier and disable transmitter */
  562.     write_scc(ctl,R5,TxCRC_ENAB | Tx8 | DTR);
  563.     /* Re-Enable SCC interrupts */
  564.     write_scc(ctl,R9,MIE|NV);        
  565.     restore(1);    /* Turn interrupts back on */
  566. }
  567.  
  568. int
  569. dohs(argc,argv,p)
  570. int argc;
  571. char *argv[];
  572. void *p;
  573. {
  574.     register int i;
  575.     register struct hdlc *hp;
  576.  
  577.     for(i=0;i<2*Nhs;i++){
  578.         hp = &Hdlc[i];
  579.         if(tprintf("port %d: txpkts %lu ints %lu rxpkts %lu rxbytes %lu nomem %lu toobig %lu crcerr %lu aborts %lu overrun %lu\n",
  580.          i,hp->txpkts,hp->exints,hp->good,hp->rxbytes,
  581.          hp->nomem,hp->toobig,hp->crcerr,hp->aborts,
  582.          hp->overrun) == EOF)
  583.             break;
  584.     }
  585.     return 0;
  586. }
  587. static int32
  588. hs_ctl(iface,cmd,set,val)
  589. struct iface *iface;
  590. int cmd;
  591. int set;
  592. int32 val;
  593. {
  594.     register struct hdlc *hp;
  595.     int32 t,ca;
  596.  
  597.     hp = &Hdlc[iface->dev];
  598.     switch(cmd){
  599.     case PARAM_TXDELAY:    /* Tx keyup delay */
  600.         if(set)
  601.             hp->txdelay = val;
  602.         return hp->txdelay;
  603.     case PARAM_PERSIST:
  604.         if(set)
  605.             hp->p = val;
  606.         return uchar(hp->p);
  607.     case PARAM_MUTE:
  608.         /* Mute transmitter for specified # of ms */
  609.         if(set){
  610.             if(val == -1){
  611.                 /* Special case for duration of a CTS */
  612.                 val = hp->txdelay + 500;
  613.             }
  614.             hp->deftime = msclock() + val;
  615.         }
  616.         t = msclock();
  617.         ca = hp->deftime - t;
  618.         if(ca < 0){
  619.             hp->deftime = t;
  620.             ca = 0;
  621.         }
  622.         return ca;
  623.     }
  624.     return -1;
  625. }
  626. #ifdef    notdef        /* replaced with assembler in 8530.asm */
  627. /* Read data from the 8530 receiver.
  628.  * Returns when either a good frame is received, or when carrier drops.
  629.  * If a good frame is received, the length is returned; otherwise -1.
  630.  */
  631. int
  632. rx8530(ctl,data,buf,bufsize)
  633. int16 ctl,data;
  634. char *buf;
  635. int16 bufsize;
  636. {
  637.     int cnt = 0;
  638.     register char status;
  639.     char error;
  640.     register char *cp = buf;
  641.  
  642.     for(;;){
  643.         status = inportb(ctl);
  644.         if(!(status & DCD)){
  645.             cnt = -1;
  646.             break;
  647.         } else if(status & BRK_ABRT){
  648.             cp = buf;
  649.             cnt = 0;
  650.         } else if(status & Rx_CH_AV){
  651.             /* Receive character is ready, get it */
  652.             *cp++ = inportb(data);
  653.             if(++cnt > bufsize){
  654.                 /* Buffer overflow, start again */
  655.                 write_scc(ctl,R3,ENT_HM|RxENABLE|RxCRC_ENAB|Rx8);
  656.                 cp = buf;
  657.                 cnt = 0;
  658.             }
  659.         } else if((error = read_scc(ctl,R1)) & END_FR){
  660.             if(!(error & CRC_ERR))
  661.                 break;    /* Good frame! */
  662.             /* Bad frame, start again */
  663.             cp = buf;
  664.             cnt = 0;
  665.         }
  666.     }
  667.     return cnt;
  668. }
  669. #endif
  670.  
  671. static int32 Del_const;
  672.  
  673. /* Find the value of Del_const that will cause one execution of mloop()
  674.  * to take one millisecond
  675.  */
  676. static void
  677. init_delay()
  678. {
  679.     int32 start,delay;
  680.     register int i,j;
  681.     int success = 0;
  682.  
  683.     /* Start with small value to make things tolerable on slow machines */
  684.     Del_const = 10;
  685.     printf("Del_const = %lu\n",Del_const);
  686.     /* Limit the number of iterations in case we don't converge */
  687.     for(i=0;i<5;i++){
  688.         start = msclock();
  689.         for(j=0;j<1000;j++)
  690.             msdelay();
  691.         delay = msclock()-start;
  692.         printf("delay %lu\n",delay);
  693.         if(delay == 0){
  694.             /* Too fast for accurate measurement on coarse clk */    
  695.             Del_const *= 10;
  696.             printf("Del_const = %lu\n",Del_const);
  697.             continue;
  698.         }
  699.         Del_const = (Del_const * 1000)/delay;
  700.         printf("Del_const = %lu\n",Del_const);
  701.         if(delay > 950 && delay < 1050){
  702.             success = 1;
  703.             break;    /* Within 1 tick - Close enough */
  704.         }
  705.     }
  706.     if(!success)
  707.         tprintf("HS: Warning: auto delay set failed\n");
  708. }
  709. /* Delay for one millisecond (once calibrated by init_delay()) */
  710. static void
  711. msdelay()
  712. {
  713.     int32 i;
  714.  
  715.     for(i=Del_const;i !=0;i--)
  716.         ;
  717. }
  718.